package com.zhoug.zmui.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.dialog.ZMUIBaseDialog;
import com.zhoug.zmui.dialog.ZMUIButtonDialog;
import com.zhoug.zmui.dialog.ZMUIDialog;
import com.zhoug.zmui.dialog.list.ZMUIAbsListDialog;
import com.zhoug.zmui.dialog.list.ZMUIMultiSelectListDialog;

import androidx.annotation.AttrRes;
import androidx.annotation.StyleRes;

/**
 * @Author 35574
 * @Date 2020/7/22
 * @Description
 */
public class DialogAttributeParser {
    private static final String TAG = ">>>DialogAttributeParser";
    private static  boolean debug = false;

    public static void setDebug(boolean debug) {
        DialogAttributeParser.debug = debug;
    }

    /**
     * {@link R.styleable#ZMUIBaseDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZMUIBaseDialogAttrs(ZMUIDialog zmuiDialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (zmuiDialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZMUIBaseDialog, defStyleAttr, defStyleRes);
        int corners = a.getDimensionPixelOffset(R.styleable.ZMUIBaseDialog_zmui_corners, 0);
        boolean fullWidth = a.getBoolean(R.styleable.ZMUIBaseDialog_zmui_fullWidth, false);
        int titleColor = a.getColor(R.styleable.ZMUIBaseDialog_zmui_titleColor, ZMUIUtils.UN_USED_COLOR);
        float titleSize = a.getDimension(R.styleable.ZMUIBaseDialog_zmui_titleSize, 0);
        int messageColor = a.getColor(R.styleable.ZMUIBaseDialog_zmui_messageColor, ZMUIUtils.UN_USED_COLOR);
        float messageSize = a.getDimension(R.styleable.ZMUIBaseDialog_zmui_messageSize, 0);
        int maxHeight = a.getDimensionPixelOffset(R.styleable.ZMUIBaseDialog_zmui_maxHeight, 0);
        a.recycle();
        if (debug) {
            LogUtils.d(TAG, "ZMUIBaseDialog:corners=" + corners);
            LogUtils.d(TAG, "ZMUIBaseDialog:fullWidth=" + fullWidth);
            LogUtils.d(TAG, "ZMUIBaseDialog:titleColor=" + Integer.toHexString(titleColor));
            LogUtils.d(TAG, "ZMUIBaseDialog:titleSize=" + titleSize);
            LogUtils.d(TAG, "ZMUIBaseDialog:messageColor=" + Integer.toHexString(messageColor));
            LogUtils.d(TAG, "ZMUIBaseDialog:messageSize=" + messageSize);
            LogUtils.d(TAG, "ZMUIBaseDialog:maxHeight=" + maxHeight);
        }
        if (zmuiDialog instanceof ZMUIBaseDialog) {
            ZMUIBaseDialog baseDialog = (ZMUIBaseDialog) zmuiDialog;
            baseDialog.setCorners(corners);
            baseDialog.setFullWidth(fullWidth);
            baseDialog.setTitleColor(titleColor);
            baseDialog.setTitleSize(titleSize);
            baseDialog.setMessageColor(messageColor);
            baseDialog.setMessageSize(messageSize);
            baseDialog.setMaxHeight(maxHeight);
        }

    }

    /**
     * {@link R.styleable#ZMUIButtonDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZMUIButtonDialogAttrs(ZMUIDialog zmuiDialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (zmuiDialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZMUIButtonDialog, defStyleAttr, defStyleRes);
        int positiveBtnColor = a.getColor(R.styleable.ZMUIButtonDialog_zmui_positiveBtnColor, ZMUIUtils.UN_USED_COLOR);
        float positiveBtnSize = a.getDimension(R.styleable.ZMUIButtonDialog_zmui_positiveBtnSize, 0);
        int negativeBtnColor = a.getColor(R.styleable.ZMUIButtonDialog_zmui_negativeBtnColor, ZMUIUtils.UN_USED_COLOR);
        float negativeBtnSize = a.getDimension(R.styleable.ZMUIButtonDialog_zmui_negativeBtnSize, 0);
        a.recycle();
        if (debug) {
            LogUtils.d(TAG, "ZMUIButtonDialog:positiveBtnSize=" + positiveBtnSize);
            LogUtils.d(TAG, "ZMUIButtonDialog:positiveBtnColor=" + Integer.toHexString(positiveBtnColor));
            LogUtils.d(TAG, "ZMUIButtonDialog:negativeBtnSize=" + negativeBtnSize);
            LogUtils.d(TAG, "ZMUIButtonDialog:negativeBtnColor=" + Integer.toHexString(negativeBtnColor));
        }
        if (zmuiDialog instanceof ZMUIButtonDialog) {
            ZMUIButtonDialog baseDialog = (ZMUIButtonDialog) zmuiDialog;
            baseDialog.setPositiveBtnColor(positiveBtnColor);
            baseDialog.setPositiveBtnSize(positiveBtnSize);
            baseDialog.setNegativeBtnColor(negativeBtnColor);
            baseDialog.setNegativeBtnSize(negativeBtnSize);
        }else if(zmuiDialog instanceof ZMUIMultiSelectListDialog){
            ZMUIMultiSelectListDialog dialog = (ZMUIMultiSelectListDialog) zmuiDialog;
            dialog.setPositiveBtnColor(positiveBtnColor);
            dialog.setPositiveBtnSize(positiveBtnSize);
            dialog.setNegativeBtnColor(negativeBtnColor);
            dialog.setNegativeBtnSize(negativeBtnSize);
        }

    }

    /**
     * {@link R.styleable#ZMUIAbsListDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZMUIAbsListDialogAttrs(ZMUIDialog zmuiDialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (zmuiDialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZMUIAbsListDialog, defStyleAttr, defStyleRes);
        float textSize = a.getDimension(R.styleable.ZMUIAbsListDialog_zmui_textSize, 0);
        int textColor = a.getColor(R.styleable.ZMUIAbsListDialog_zmui_textColor, ZMUIUtils.UN_USED_COLOR);
        int dividerHeight = a.getDimensionPixelOffset(R.styleable.ZMUIAbsListDialog_zmui_dividerHeight, 1);
        int dividerColor = a.getColor(R.styleable.ZMUIAbsListDialog_zmui_dividerColor, Color.parseColor("#dddddd"));
        boolean showDivider = a.getBoolean(R.styleable.ZMUIAbsListDialog_zmui_showDivider, true);
        a.recycle();
        if (debug) {
            LogUtils.d(TAG, "ZMUIAbsListDialog:textSize=" + textSize);
            LogUtils.d(TAG, "ZMUIAbsListDialog:textColor=" + Integer.toHexString(textColor));
            LogUtils.d(TAG, "ZMUIAbsListDialog:dividerHeight=" + dividerHeight);
            LogUtils.d(TAG, "ZMUIAbsListDialog:dividerColor=" + Integer.toHexString(dividerColor));
            LogUtils.d(TAG, "ZMUIAbsListDialog:showDivider=" +showDivider);
        }
        if (zmuiDialog instanceof ZMUIAbsListDialog) {
            ZMUIAbsListDialog<?> dialog = (ZMUIAbsListDialog<?>) zmuiDialog;
            dialog.setTextSize(textSize);
            dialog.setTextColor(textColor);
            dialog.setDividerColor(dividerColor);
            dialog.setDividerHeight(dividerHeight);
            dialog.setShowDivider(showDivider);
        }

    }

}
